package org.jeecg.modules.deepseek.tools;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.demo.wuyou.service.ISitemapService;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

@Slf4j
@Configuration
public class WebMysqlTools {

    @Autowired
    ISitemapService sitemapService;

    public record TableNameQuery( @Schema(description = "表名称")String TableName) {

    }


    public record QueryTableSelector( @Schema(description = "查询sql")String TableSQL) {

    }

    @Bean
    @Description("返回目前可以管理的数据库")
//    @Tool()
    public Supplier<List<Map<String, Object>>> getManageableTableStructures() {
        return () -> {
            try {
                List<Map<String, Object>> maps = sitemapService.GetByCondition("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES  WHERE TABLE_SCHEMA = 'jeecg-boot' AND TABLE_NAME LIKE 'web%'; ", new HashMap<String, Object>());
                log.debug("查询结果{}", maps);
                return maps;
            } catch (Exception e) {
//                log.warn("查询失败{},{}", request.TeacherName(), e.getMessage());
                return null;
            }
        };
    }



    @Bean
    @Description("根据表结构返回对应的查询sql（不要使用SELECT *），来进行查询数据")
    public Function<QueryTableSelector, List<Map<String, Object>>> getTableSelector()  {
        return request -> {
            try {

                List<Map<String, Object>> maps = sitemapService.GetByCondition(request.TableSQL, new HashMap<String, Object>());
                log.debug("查询结果{}", maps);
                return maps;
            } catch (Exception e) {
//                log.warn("查询失败{},{}", request.TeacherName(), e.getMessage());
                return null;
            }
        };
    }
    @Bean
    @Description("根据表名称返回对应的表结构")
    public Function<TableNameQuery, List<Map<String, Object>>> getTableSchema() {
        return request -> {
            try {
                String databaseName = "jeecg-boot"; // 替换为你的数据库名称
                String tableName = request.TableName; // 替换为你的表名称
                String str = "SELECT \n" +
                        "    COLUMN_NAME AS '列名',\n" +
                        "    COLUMN_TYPE AS '数据类型',\n" +
                        "    IS_NULLABLE AS '是否允许为空',\n" +
                        "    COLUMN_KEY AS '键类型',\n" +
                        "    COLUMN_DEFAULT AS '默认值',\n" +
                        "    EXTRA AS '额外信息',\n" +
                        "    COLUMN_COMMENT AS '列注释'\n" +
                        "FROM \n" +
                        "    information_schema.COLUMNS\n" +
                        "WHERE \n" +
                        "    TABLE_SCHEMA = '%s'\n" +
                        "    AND TABLE_NAME = '%s';\n";

                String query = String.format(str, databaseName, tableName);
                List<Map<String, Object>> maps = sitemapService.GetByCondition(query, new HashMap<String, Object>());
                log.debug("查询结果{}", maps);
                return maps;
            } catch (Exception e) {
//                log.warn("查询失败{},{}", request.TeacherName(), e.getMessage());
                return null;
            }
        };
    }
}
